package com.example.james.restartapptest;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Process;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showPidInfo();
        Button button = findViewById(R.id.restartButton);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                restartAppNewProcessByHandler();
            }
        });

        Button restartActivityBtn = findViewById(R.id.restartActivity);
        restartActivityBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                restartActivity();
            }
        });

        Button restartAppByAlarmBtn = findViewById(R.id.restartByAlarm);
        restartAppByAlarmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                restartAppNewProcess();
            }
        });
    }

    private void restartActivity() {
        Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    private void restartAppNewProcess() {
        Intent mStartActivity = new Intent(MainActivity.this, MainActivity.class);
        int mPendingIntentId = 0;
        PendingIntent mPendingIntent = PendingIntent.getActivity(MainActivity.this, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager mgr = (AlarmManager) MainActivity.this.getSystemService(Context.ALARM_SERVICE);
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
        System.exit(0);
    }

    private void restartAppNewProcessByHandler() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(MainActivity.this, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                Process.killProcess(Process.myPid());
                System.exit(0);
            }
        }, 500);
    }

    private void showPidInfo(){
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        String prePid = sharedPreferences.getString("PrePid", "");
        TextView prePidText = findViewById(R.id.prePidText);
        prePidText.setText("Pre PID: " + prePid);
        SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
        editor.putString("PrePid", Process.myPid() + "");
        editor.apply();
        TextView currentPidText = findViewById(R.id.currentPidText);
        currentPidText.setText("Cur PID: " + Process.myPid());
    }
}
